﻿using HZU.ZP.Data.Interfaces;
using HZU.ZP.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HZU.ZP.Data.Impl
{
    public class ApplyRecordDao:IApplyRecordDao
    {
        private EFCoreDbContext _context;

        public ApplyRecordDao(EFCoreDbContext context)
        {
            _context = context;
        }

        public void Add(ApplyRecord applyRecord)
        {
            _context.ApplyRecords.Add(applyRecord);
            _context.SaveChanges();
        }

        public ApplyRecord Get(int id)
        {
            return _context.ApplyRecords.FirstOrDefault(e => e.Id == id);
        }

        public ApplyRecord Get(int applicantId, int positionId)
        {
            return _context.ApplyRecords.FirstOrDefault(e => e.ApplicantId == applicantId && e.PositionId == positionId);
        }

        public int GetAppCount(int applicantId, int processing)
        {
            return _context.ApplyRecords.Where(e => e.ApplicantId == applicantId && e.Processing == processing).Count();
        }

        public IList<ApplyRecord> GetByApplicant(int applicantId, int processing, int pageIndex)
        {
            return _context.ApplyRecords.Where(e => e.ApplicantId == applicantId && e.Processing == processing).OrderByDescending(e => e.CreateTime).Skip((pageIndex - 1) * 20).Take(20).ToList();
        }

        public ApplyRecord GetByComAndId(int companyId, int id)
        {
            return _context.ApplyRecords.FirstOrDefault(e => e.CompanyId == companyId && e.Id == id);
        }

        public IList<ApplyRecord> GetByCompany(int companyId, int processing, int pageIndex)
        {
            return _context.ApplyRecords.Where(e => e.CompanyId == companyId && e.Processing == processing).Skip((pageIndex - 1) * 20).Take(20).ToList();
        }

        public int GetComCount(int companyId, int processing)
        {
            return _context.ApplyRecords.Where(e => e.CompanyId == companyId && e.Processing == processing).Count();
        }

        public void Update(ApplyRecord applyRecord)
        {
            _context.ApplyRecords.Update(applyRecord);
            _context.SaveChanges();
        }
    }
}
